package com.jbinfo.qingcaowan.user.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jbinfo.common.utils.DataTables;
import com.jbinfo.common.utils.UUIDUtils;
import com.jbinfo.qingcaowan.user.dao.UserManualDao;
import com.jbinfo.qingcaowan.user.domain.UserManual;
import com.jbinfo.qingcaowan.user.dto.UserManualDTO;
@Service("manualService")
public class ManualServiceImpl implements ManualService{

	@Resource(name="userManualDao")
	private UserManualDao userManualDao;
	
	@Override
	public DataTables<UserManualDTO> findByParam(UserManualDTO userManualDTO) {
		// TODO Auto-generated method stub
//		userManualModel.setAuthorityOrgs(Securitys.getAuthorityOrgs());
//		userManualDTO.set
		List<UserManualDTO> nList = userManualDao.findByParam(userManualDTO);
		return DataTables.combinTotalRecords(nList);
	}

	@Override
	public void save(UserManual userManual){
		verification(userManual,"create");
		String id = UUIDUtils.create();
		userManual.setId(id);
		userManual.setCreatTime(new Date());
		userManual.setCreatUser(userManual.getCreatUser());
		userManualDao.save(userManual);
		
	}

	@Override
	public UserManualDTO getById(String id) {
		// TODO Auto-generated method stub
		return userManualDao.getById(id);
	}

	@Override
	public void edit(UserManual userManual) {
		// TODO Auto-generated method stub
		verification(userManual,"update");
		userManual.setModfiyTime(new Date());
		userManual.setModfiyUser(userManual.getModfiyUser());
		userManualDao.update(userManual);
	}

	@Override
	public void delete(String id) {
		userManualDao.delete(id);
	}

	private void verification(UserManual userManual,String type){
		if("update".equals(type)&&(userManual.getId() == null || "".equals(userManual.getId()))){
			throw new RuntimeException("手册id不能为空;");
		}
		if(userManual.getOrgId() == null || "".equals(userManual.getOrgId())){
			throw new RuntimeException("适应单位不能为空;");
		}
		if(userManual.getManualContent() == null || "".equals(userManual.getManualContent())){
			throw new RuntimeException("手册内容不能为空;");
		}
	}
	
	// 查询学员手册内容
	@Override
	public String findManualContent(UserManualDTO userManualDTO) {

		return userManualDao.findManualContent(userManualDTO);
	}
	

	public UserManualDao getUserManualDao() {
		return userManualDao;
	}

	public void setUserManualDao(UserManualDao userManualDao) {
		this.userManualDao = userManualDao;
	}

}
